const UserService = require('./service/user.service')
const { join } = require('path')
const { BranchItem } = require('./model/user')
const TyGit = require('ty-git-crud')

// 每次提交代码后，调用这个服务，将新提交代码的信息同步到数据库
// 用户名在本系统中也是唯一的
// !这个函数必须在用户创建仓库后，推送代码时调用
exports.loadRepoInfo = async function loadRepoInfo(repos, username, repoName, branchName) {
    const userService = new UserService()
    const tyGit = new TyGit(join(repos.dirMap(), username))
    const user = await userService.findOneByUsername(username)
    if (user) {
        const repos = user.repos
        // 查看用户提交的代码库是否在数据库中
        // 这里的用意其实是抛开node-server的根据文件目录验证
        // 再做一次数据库中的验证
        const repoIndex = repos.findIndex(i => i.name === repoName.replace(/\.git$/, '')) // 数据库中的并没有存后缀，所以这里要删除
        if (repoIndex !== -1) {
            const repo = repos[repoIndex]
            const branch = repo.branch

            // 这个commit_set就是最新的。
            const commit_set = await tyGit.allCommit(repoName, branchName)
            const lastest_commit_id = commit_set[0].commitHash
            const latest_time = commit_set[0].time
            const latest_relative_time = commit_set[0].relative_time

            // 这俩个时间是成对赋值的
            // 仓库初始化的时候，没有值
            if (!repo.updated_time && !repo.relative_time) {
                repo.updated_time = latest_time
                repo.relative_time = latest_relative_time
            }

            // 比较仓库最后更新时间
            const repoDate = new Date(repo.updated_time)
            const theCommitDate = new Date(latest_time)
            if (repoDate < theCommitDate) {
                repo.updated_time = latest_time;
                repo.relative_time = latest_relative_time
            }

            // 分支存在 
            if (branch[branchName]) {
                const branchItem = branch[branchName]
                // 获取增量的hashmap
                const file_commit_hashmap = await tyGit.partCommitDetailByBranch(repoName, branchName, branchItem.lastest_commit_id)
                branchItem.commit_set = commit_set
                branchItem.file_commit_hashmap = Object.assign(branchItem.file_commit_hashmap, file_commit_hashmap)
                branchItem.lastest_commit_id = lastest_commit_id
            } else {
                // 分支不存在，就要创建分支
                const branchItem = new BranchItem(branchName)
                branch[branchName] = branchItem
                const file_commit_hashmap = await tyGit.allCommitDetailByBranch(repoName, branchName)
                branchItem.commit_set = commit_set
                branchItem.file_commit_hashmap = file_commit_hashmap
                branchItem.lastest_commit_id = commit_set[0].commitHash
            }
            await userService.create(user) // 更新
        }



        // if (repoIndex !== -1) {
        //     const repo = repos[repoIndex]
        //     const branch = repo.branch
        //     // 分支存在
        //     if (branch[branchName]) {
        //         const branchItem = branch[branchName]
        //         // 这个commit_set就是最新的。
        //         const commit_set = await tyGit.allCommit(repoName, branchName)
        //         const lastest_commit_id = commit_set[0].commitHash
        //         const latest_time = commit_set[0].time
        //         const latest_relative_time = commit_set[0].relative_time

        //         // 比较仓库最后更新时间
        //         const repoDate = new Date(repo.updated_time)
        //         const theCommitDate = new Date(latest_time)
        //         if (repoDate < theCommitDate) {
        //             repo.updated_time = latest_time;
        //             repo.relative_time = latest_relative_time
        //         }
                
        //         // 获取增量的hashmap
        //         const file_commit_hashmap = await tyGit.partCommitDetailByBranch(repoName, branchName, branchItem.lastest_commit_id)
        //         branchItem.commit_set = commit_set
        //         branchItem.file_commit_hashmap = Object.assign(branchItem.file_commit_hashmap, file_commit_hashmap)
        //         branchItem.lastest_commit_id = lastest_commit_id

        //     } else { // 分支不存在
        //         const branchItem = new BranchItem(branchName)
        //         branch[branchName] = branchItem
        //         const commit_set = await tyGit.allCommit(repoName, branchName)
        //         const file_commit_hashmap = await tyGit.allCommitDetailByBranch(repoName, branchName)
        //         const latest_time = commit_set[0].time
        //         const latest_relative_time = commit_set[0].relative_time

        //         // 虽然是第一次推送，但是可能已经推送了其他分支，所以还是要比较
        //         const repoDate = new Date(repo.updated_time)
        //         const theCommitDate = new Date(latest_time)
        //         if (repoDate < theCommitDate) {
        //             repo.updated_time = latest_time;
        //             repo.relative_time = latest_relative_time
        //         }

        //         branchItem.commit_set = commit_set
        //         branchItem.file_commit_hashmap = file_commit_hashmap
        //         branchItem.lastest_commit_id = commit_set[0].commitHash
        //     }
        //     await userService.create(user) // 更新
    }
}